﻿using UnityEngine;
using UnityEditor;
using System.IO;

namespace SoarDEditor.Pandora
{
    public class ImporterSprite
    {
        static bool IsSprite(string file)
        {
            string filename = file.ToLower();
            return filename.EndsWith("png") || filename.EndsWith("jpg");
        }
        static bool IsPOT(Texture texture)
        {
            return Mathf.IsPowerOfTwo(texture.width) && Mathf.IsPowerOfTwo(texture.height);
        }
        public static int GetMaxSize(Texture texture)
        {
            int size = Mathf.Max(texture.width, texture.height);
            return Mathf.Clamp(Mathf.NextPowerOfTwo(size), 32, 8192);
        }

        //    // 获得纹理格式
        //    public static TextureImporterFormat GetFormat(Texture texture)
        //    {
        //        TextureImporterFormat format = TextureImporterFormat.AutomaticTruecolor;
        //
        //        bool isPOT = IsPOT (texture);
        //        if (isPOT) {
        //            //switch (texture.format) {
        //            //case TextureFormat.RGB24:
        //            //    format = TextureImporterFormat.AutomaticCompressed;
        //            //    break;
        //            //}
        //            format = TextureImporterFormat.AutomaticCompressed;
        //        }
        //
        //        return format;
        //    }

        static void ReimportFile(string path, string tag)
        {
            if (!File.Exists(path))
            {
                return;
            }

            TextureImporterCompression textureFormat = TextureImporterCompression.Uncompressed;
            TextureImporterType type = TextureImporterType.Sprite;
            TextureWrapMode mode = TextureWrapMode.Clamp;
            if (path.Contains("_repeat"))
            {
                //type = TextureImporterType.Advanced;
                mode = TextureWrapMode.Repeat;
            }

            TextureImporter importer = AssetImporter.GetAtPath(path) as TextureImporter;
            System.Diagnostics.Debug.Assert(importer != null, "importer != null");
            if (importer.textureType != type ||
                importer.mipmapEnabled ||
                importer.textureCompression != TextureImporterCompression.Uncompressed ||
                importer.wrapMode != mode ||
                (!string.IsNullOrEmpty(tag) && importer.spritePackingTag != tag))
            {
                importer.textureType = type;
                importer.mipmapEnabled = false;
                importer.textureCompression = textureFormat;
                importer.spritePackingTag = tag;
                importer.wrapMode = mode;

                importer.SaveAndReimport();
            }

            //        Texture2D texture = AssetDatabase.LoadAssetAtPath<Texture2D> (path);
            int textureSize = 2048;//GetMaxSize (texture);
            if (importer.maxTextureSize != textureSize)
            {
                importer.maxTextureSize = textureSize;
                importer.SaveAndReimport();
            }


            importer.ClearPlatformTextureSettings("iPhone");
            importer.ClearPlatformTextureSettings("Android");

            //		int maxSize = 2048;  
            //		TextureImporterFormat format = TextureImporterFormat.AutomaticTruecolor;  
            //		int quality = 50;  
            //		importer.GetPlatformTextureSettings("Android", out maxSize, out format, out quality);  
            //
            //		// 压缩的格式，android下修改为分离alpha通道的etc1  
            //		if (format == TextureImporterFormat.AutomaticTruecolor)  
            //		{  
            //			importer.SetPlatformTextureSettings("Android", maxSize, TextureImporterFormat.AutomaticCompressed, quality, true);
            //			importer.SaveAndReimport();
            //		}

            //		importer.GetPlatformTextureSettings("iPhone", out maxSize, out format, out quality);  
            //		if (format == TextureImporterFormat.AutomaticCompressed || format == TextureImporterFormat.ARGB32) {
            //			importer.SetPlatformTextureSettings("iPhone", 2048, TextureImporterFormat.AutomaticTruecolor);
            //			importer.SaveAndReimport();
            //		}
        }

        public static void ReimportPath(string path, string tag)
        {
            if (!Directory.Exists(path))
            {
                return;
            }

            string[] dirs = Directory.GetDirectories(path);
            for (int i = 0; i < dirs.Length; ++i)
            {
                ReimportPath(dirs[i], tag);
            }

            string[] files = Directory.GetFiles(path);
            for (int i = 0; i < files.Length; ++i)
            {
                string file = files[i];
                if (IsSprite(file))
                {
                    ReimportFile(file, tag);
                }
            }
        }

        static void ReimportDirectory(string path)
        {
            if (!Directory.Exists(path))
            {
                return;
            }

            string[] dirs = Directory.GetDirectories(path);
            for (int i = 0; i < dirs.Length; ++i)
            {
                string dir = dirs[i];

                string name = dir.Replace(path, "").Replace("\\", "/");
                string[] items = name.Split(new[] { '/' }, System.StringSplitOptions.RemoveEmptyEntries);
                ReimportPath(dir, items[0]);
            }
        }

        static void ReimportAtlas()
        {
            ReimportDirectory(Config.UI_ASSETS_PATH + "/atlas/");
            //ReimportDirectory (Config.UI_ASSETS_PATH + "/atlas_changeSkin/");
        }

        static void ReimportImage()
        {
            ReimportPath(Config.UI_ASSETS_PATH + "/image/", "");
        }

        static void ReimportTexture()
        {
            ReimportPath(Config.UI_ASSETS_PATH + "/texture/", "");
        }

        //public static void ReimportAnimation ()
        //{
        //    ReimportPath ("Assets/Data/animation/2d/image/", "animation_image");
        //}

        public static void Reimport()
        {
            ReimportAtlas();
            ReimportImage();
            ReimportTexture();
            //ReimportAnimation ();

            AssetDatabase.Refresh();
            Debug.Log("Sprite Reimport Completed");
        }

        public static void ReimportSelect(Object item)
        {
            if (!(item is Texture2D))
            {
                return;
            }

            string path = AssetDatabase.GetAssetPath(item);
            ReimportFile(path, "");
        }
    }
}